// index page
// ============
define(['require', 'zepto', 'wx', 'utils', 'qiniu', 'qiniuPlayer', 'fx', 'flipclock', 'sui', 'smExtend'], function(require, $, wx, utils, qiniu, QPlayer) {
  return {
    init : function(data) {
      
//      $('.flipclock').flipclock({
//        countdown: '03/11/2019 23:59:59'
//      });
    	
      $(".swiper-container").swiper();
      $(document).on("click", ".supporter-layout .supporter-box", function() {
        if($(this).is(".active")) {
          $(this).removeClass("active")
          $(this).animate({
            border: '1px solid transparent',
          }, 2000, 'ease-out')
          $(this).find(".content-item").animate({
            opacity: 0,
            width: '0',
            display: 'none'
          }, 2000)
        } else {
          $(this).addClass("active").siblings().removeClass("active")
          $(this).siblings().animate({
            border: '1px solid transparent',
          }, 2000)
          $(this).siblings().find(".content-item").animate({
            opacity: 0,
            width: '0',
            display: 'none'
          }, 2000)
          
          $(this).animate({
            border: '1px solid #d11b28',
          }, 2000, 'ease-out')
          $(this).find(".content-item").animate({
            opacity: 1,
            width: 'auto',
            display: 'inline-block'
          }, 2000)
        }
      })
    	
      var end = $('.flipclock').data("end")
      $('.flipclock').flipclock({
        'countdown': end
      });
      
      if(data.disableShare == 'true') {
        utils.configWeixin(data.jsConfig, false)
      } else if (data.status){
        if(data.status == 'OPEN' || data.status == 'OPENFAILED' || data.status == 'PROCESSING' || data.status == 'CLOSE' || data.status == 'CLOSEFAILED') {
          
          var name = data.userName ? data.userName + "28天减重5%，敢不敢跟我赌？" : "【赌你会瘦】28天减重5%，敢不敢跟我赌？";
          var desc = "信或不信，巨额奖金都在那里，等你来瓜分！"
          utils.configWeixin(data.jsConfig, true, {
            name: name,
            desc: desc,
            link: data.url ? data.url : window.__CTX__.domain + "/welcome",
            cover : data.avatar
          });
          
        } else {
          utils.configWeixin(data.jsConfig, true, {
            name: "【赌你会瘦】28天减重5%，敢不敢跟我赌？",
            desc: "信或不信，巨额奖金都在那里，等你来瓜分！",
            link: window.__CTX__.domain + "/welcome",
            cover : data.avatar
          });
        }
      } else {
        utils.configWeixin(data.jsConfig, true, {
          name: "【赌你会瘦】28天减重5%，敢不敢跟我赌？",
          desc: "信或不信，巨额奖金都在那里，等你来瓜分！",
          link: window.__CTX__.domain + "/welcome",
          cover : data.avatar
        });
      }
      
      if(data.reloadToOpen == 'true') {
        setTimeout(function(){
          window.location.href= $("#redirect").attr("href");
        }, 3000)
      }
      
      if(data.reloadToSuccess == 'true') {
        setTimeout(function(){
          $(".join-button").removeClass("hide");
          $("#success").removeClass("hide");
          $("#waiting").addClass("hide");
        }, 3000)
      }
      
      if(data.endDate) {
        console.log(data.endDate);
//        $('#clock').countdown(data.endDate, function(event) {
//          $(this).html(event.strftime('%D days %H:%M:%S'));
//        });
//        
//        var second=5;
//        var date20before=new Date();
//        date20before.setSeconds(date20before.getSeconds()+second);
//        $('#clock').countDown({
//            targetDate: {
//                'year': date20before.getFullYear(),
//                'month': date20before.getMonth() + 1,
//                'day': date20before.getDate(),
//                'hour': date20before.getHours(),
//                'min': date20before.getMinutes(),
//                'sec': date20before.getSeconds()
//            },
//            onTicktock: function(diffSec){
//                console.log('#clock onTicktock:'+diffSec);    //console diff second;
//            },
//            onComplete: function () {
//                console.log('#clock onComplete,and stop #time_bx2 countdown');
//            }
//        });
      }
      
      var currentVideo = $("#uploadVideo").data("video");
      if(currentVideo) {
        this.player = new QPlayer({
          url: currentVideo + '?avvod/m3u8/s/960x640/vb/1000k',
          container: document.getElementById("uploadVideo"),
          defaultViewConfig: {
            showControls: true,
          }
        });
      }
      
      var currentAfterVideo = $("#uploadAfterVideo").data("video");
      if(currentVideo) {
        this.afterplayer = new QPlayer({
          url: currentAfterVideo,
          container: document.getElementById("uploadAfterVideo"),
          defaultViewConfig: {
            showControls: true,
          }
        });
      }
      
      $(".open-more").on("click", function(e){
        var target = $(this).data("target");
        $(this).addClass("hide");
        $(target).removeClass("hide");
      })
      
    }
  }
})